package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "TRAMO", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Tramo.findAll", query = "SELECT t FROM Tramo t"),
    @NamedQuery(name = "Tramo.findById", query = "SELECT t FROM Tramo t WHERE t.id = :id")})
public class Tramo extends EntityBean<Tramo> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tramo")
    private final ChangeSupportList<Caseta> casetaList = new ChangeSupportList<Caseta>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tramo")
    private final ChangeSupportList<Gasolineria> gasolineriaList = new ChangeSupportList<Gasolineria>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tramo")
    private final ChangeSupportList<ZonaRiesgo> zonaRiesgoList = new ChangeSupportList<ZonaRiesgo>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tramo")
    private final ChangeSupportList<Retorno> retornoList = new ChangeSupportList<Retorno>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tramo")
    private final ChangeSupportList<Entronque> entronqueList = new ChangeSupportList<Entronque>();
    @JoinColumn(name = "ubicaciontermina", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Ubicacion ubicacion;
    @JoinColumn(name = "ubicacioninicia", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Ubicacion ubicacion1;
    @JoinColumn(name = "responsable", referencedColumnName = "numero", nullable = false)
    @ManyToOne(optional = false)
    private Empleado empleado;
    @JoinColumn(name = "carretera", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Carretera carretera;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tramo")
    private final ChangeSupportList<Poblado> pobladoList = new ChangeSupportList<Poblado>();

    public Tramo() {
    }

    public Tramo(Integer id) {
        this.id = id;
    }

    public Tramo(Integer id, List<Caseta> casetaList, List<Gasolineria> gasolineriaList, List<ZonaRiesgo> zonaRiesgoList, List<Retorno> retornoList, List<Entronque> entronqueList, Ubicacion ubicacion, Ubicacion ubicacion1, Empleado empleado, Carretera carretera, List<Poblado> pobladoList) {
        this.id = id;
        mergeListContent(this.casetaList, casetaList);
        mergeListContent(this.gasolineriaList, gasolineriaList);
        mergeListContent(this.zonaRiesgoList, zonaRiesgoList);
        mergeListContent(this.retornoList, retornoList);
        mergeListContent(this.entronqueList, entronqueList);
        this.ubicacion = ubicacion;
        this.ubicacion1 = ubicacion1;
        this.empleado = empleado;
        this.carretera = carretera;
        mergeListContent(this.pobladoList, pobladoList);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        support.fireChange();
    }

    public List<Caseta> getCasetaList() {
        return casetaList;
    }

    public void setCasetaList(List<Caseta> casetaList) {
        mergeListContent(this.casetaList, casetaList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public List<Gasolineria> getGasolineriaList() {
        return gasolineriaList;
    }

    public void setGasolineriaList(List<Gasolineria> gasolineriaList) {
        mergeListContent(this.gasolineriaList, gasolineriaList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public List<ZonaRiesgo> getZonaRiesgoList() {
        return zonaRiesgoList;
    }

    public void setZonaRiesgoList(List<ZonaRiesgo> zonaRiesgoList) {
        mergeListContent(this.zonaRiesgoList, zonaRiesgoList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public List<Retorno> getRetornoList() {
        return retornoList;
    }

    public void setRetornoList(List<Retorno> retornoList) {
        mergeListContent(this.retornoList, retornoList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public List<Entronque> getEntronqueList() {
        return entronqueList;
    }

    public void setEntronqueList(List<Entronque> entronqueList) {
        mergeListContent(this.entronqueList, entronqueList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    public Ubicacion getUbicacion() {
        return ubicacion;
    }

    public void setUbicacion(Ubicacion ubicacion) {
        this.ubicacion = ubicacion;
        support.fireChange();
    }

    public Ubicacion getUbicacion1() {
        return ubicacion1;
    }

    public void setUbicacion1(Ubicacion ubicacion1) {
        this.ubicacion1 = ubicacion1;
        support.fireChange();
    }

    public Empleado getEmpleado() {
        return empleado;
    }

    public void setEmpleado(Empleado empleado) {
        this.empleado = empleado;
        support.fireChange();
    }

    public Carretera getCarretera() {
        return carretera;
    }

    public void setCarretera(Carretera carretera) {
        this.carretera = carretera;
        support.fireChange();
    }

    public List<Poblado> getPobladoList() {
        return pobladoList;
    }

    public void setPobladoList(List<Poblado> pobladoList) {
        mergeListContent(this.pobladoList, pobladoList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Tramo)) {
            return false;
        }
        Tramo other = (Tramo) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("%s(%s-%s)",carretera,ubicacion,ubicacion1);
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        casetaList.addChangeListener(listener);
        entronqueList.addChangeListener(listener);
        gasolineriaList.addChangeListener(listener);
        pobladoList.addChangeListener(listener);
        retornoList.addChangeListener(listener);
        zonaRiesgoList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public Tramo clone() {
        return new Tramo(id, casetaList, gasolineriaList, zonaRiesgoList, retornoList, entronqueList, ubicacion, ubicacion1, empleado, carretera, pobladoList);
    }

    @Override
    public void merge(Tramo other) {
        this.id = other.id;
        mergeListContent(this.casetaList, other.casetaList);
        mergeListContent(this.gasolineriaList, other.gasolineriaList);
        mergeListContent(this.zonaRiesgoList, other.zonaRiesgoList);
        mergeListContent(this.retornoList, other.retornoList);
        mergeListContent(this.entronqueList, other.entronqueList);
        this.ubicacion = other.ubicacion;
        this.ubicacion1 = other.ubicacion1;
        this.empleado = other.empleado;
        this.carretera = other.carretera;
        mergeListContent(this.pobladoList, other.pobladoList);
    }

}
